package jinyilw.swing.rlaf.ui;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.graphics.GraphicsTools;
import jinyilw.swing.graphics.RenderingConfig;

import javax.swing.JButton;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 * @author Jinyi
 */
public class RComboArrowButton extends JButton
{
	private static final long serialVersionUID = 5795094451409929034L;
	protected int direction;
	protected Color arrowColor;

	public RComboArrowButton(int direction)
	{
		super();
		setRequestFocusEnabled(false);
		setOpaque(false);
		setDirection(direction);
		arrowColor = getForeground();
	}

	public int getDirection()
	{
		return direction;
	}

	public void setDirection(int direction)
	{
		this.direction = direction;
	}

	public void setArrowColor(Color color)
	{
		if (arrowColor != color)
		{
			arrowColor = color;
			// repaint();
		}
	}

	private final RenderingConfig renderingConfig = new RenderingConfig();
	@Override
	public void paint(Graphics g)
	{

		int size;
		int h = getHeight();
		int w = getWidth();
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = ComponentTools.applyEnableAlpha(g2d, isEnabled(),
				0.9f, 0.2f);
		renderingConfig.setAAPainting(g2d);
		size = Math.min((h - 4) / 2, (w - 4) / 2);
		size = Math.max(size, 2) / 2 * 2;
		g2d.setColor(arrowColor);
		GraphicsTools.fillTriangle(g2d, 2, (h - size) / 2, size, size,
				direction);
		renderingConfig.resetAAPainting(g2d);
		if (composite != null)
			g2d.setComposite(composite);
	}

	@Override
	public void updateUI()
	{
		// setUI(new BasicButtonUI());
	}

	@Override
	public Dimension getPreferredSize()
	{
		return new Dimension(16, 16);
	}

	@Override
	public Dimension getMinimumSize()
	{
		return new Dimension(5, 5);
	}

	@Override
	public Dimension getMaximumSize()
	{
		return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
	}

	@Override
	public boolean isFocusTraversable()
	{
		return false;
	}
}
